﻿var loading = new load();
loading.show("/Prices/Load", 1);
function showlist(loadstr) {
    var str;
    var s = loadstr["page"];
    showpage(s, "/Prices/Load");
    var e = loadstr["list"];
    for (var i = 0; i < e.length; i++) {
        str = str + "<tr><td><input id='cbox' data-id='" + e[i].prices_id + "'data-key='prices_id' type='checkbox' /></td>" +
        "<td title='套餐价格' data-key='prices_name'>"+ e[i].prices_name + "</td>" +
        "<td title='普通价格' data-key='prices_money'>" + e[i].prices_money + "</td>" +
        "<td title='会员价格' data-key='prices_vip'>" + e[i].prices_vip + "</td>" +
        "<td title='备注' data-key='prices_remake'>" + e[i].prices_remake + "</td></tr>";
    }
    $(".loading").hide();
    return str;
}
//改变页码的时候
function pg_change(page) {
    if (isNaN(page)) {
        layer.msg("请数字");
        return;
    } else if (page <= 0) {
        layer.msg("请输入大于等于1的页数");
        return;
    }
    loading.show("/Prices/Load", page);
}
//获取选择的数据
function date() {
    var lists = [];
    //var list=[user_id];
    $("input[id='cbox']:checked").each(function (i) {
        var list = {};
        list.prices_id = $(this).attr("data-id");
        list.prices_name = $(this).parent().parent().find("td[data-key='prices_name']").text();
        list.prices_money = $(this).parent().parent().find("td[data-key='prices_money']").text();
        list.prices_vip = $(this).parent().parent().find("td[data-key='prices_vip']").text();
        list.prices_remake = $(this).parent().parent().find("td[data-key='prices_remake']").text();
        lists.push(list);
    })
    return lists;
}
//添加
function add() {
    layer.open({
        type: 1,
        skin: 'layui-layer-rim',
        title: "添加",
        area: ['350px', '300px'],
        content: $('#Dlgshow'),
        btn: ["保存", "关闭"],
        yes: function (index, layero) {
            var list = {};
            if (!Dlgshow())
                return;
            list.prices_name = $(layero).find("input[name='name']").val();
            list.prices_money = $(layero).find("input[name='money']").val();
            list.prices_vip = $(layero).find("input[name='vip']").val();
            console.log(list.prices_vip);
            $.post("/Prices/Add", { json: JSON.stringify(list) }, function (date) {
                console.log(date);
                if (date.s == "ok") {
                    layer.msg(date.msg);
                } else {
                    layer.msg(date.msg);
                }
                loading.show("/Prices/Load", 1);
                console.log("---");
            }, "json");
            layer.close(index);
        },
        cancel: function (index) {
            layer.close(index);

        }
    });
}
//编辑
function edit() {
    var lists = date();
    if (lists.length == 0) {
        layer.msg("请选择一条哟！！");
        return;
    } else if (lists.length != 1) {
        layer.msg("一条数据，不要贪杯哦");
    } else {
        $("#Dlgshow").find("input[name='name']").val(lists[0].prices_name);
        $("#Dlgshow").find("input[name='money']").val(lists[0].prices_money);
        $("#Dlgshow").find("input[name='vip']").val(lists[0].prices_vip);
        layer.open({
            type: 1,
            skin: 'layui-layer-rim',
            title: "用户编辑",
            area: ['350px', '300px'],
            content: $('#Dlgshow'),
            btn: ["保存", "关闭"],
            yes: function (index, layero) {
                if (!Dlgshow())
                    return;
                console.log("猜猜我执行没有")
                lists[0].prices_name = $(layero).find("input[name='name']").val();
                lists[0].prices_money = $(layero).find("input[name='money']").val();
                lists[0].prices_vip = $(layero).find("input[name='vip']").val();
                $.post("/Prices/Edit", { json: JSON.stringify(lists[0]) }, function (date) {
                    console.log(date);
                    layer.msg(date.msg);
                    loading.show("/Prices/Load", 1);
                }, "json");
                layer.close(index);
            },
            cancel: function (index) {
                layer.close(index);

            }
        });
    }
}
//删除
function del() {
    var lists = date();
    if (lists.length == 0) {
        layer.msg("请至少选择一条哟！！");
        return;
    }
    layer.confirm('确定取消选中信息？', {
        btn: ['确定', '取消'] //按钮
    }, function () {
        $.post("/Prices/Del", { json: JSON.stringify(lists) }, function (date) {
            console.log(date);
            layer.msg(date.msg);
            loading.show("/Prices/Load", 1);
            console.log("---");
        }, "json");
    });
}
//编辑框验证
function Dlgshow() {
    var $d=$("#Dlgshow");
    var name = $d.find("input[name='name']").val();
    var mony = $d.find("input[name='money']").val();
    var vip = $d.find("input[name='vip']").val();
    if (name == "" || mony==""||vip=="") {
        layer.msg("不能为空");
        return false;
    } else if (isNaN(mony)||isNaN(vip)) {
        layer.msg("价格请输入数字");
        return false;
    }
    return true;
}